/* 
** (Copyright) sejishikong <sejishikong@263.net>
** Authors:    sejishikong <sejishikong@263.net>
**             Wei Lian <lianwei3@gmail.com> 
** Maintainer: Wei Lian
**    This program is free software; you can redistribute it and/or modify
**    it under the terms of the GNU General Public License Version 2 as
**    published by the Free Software Foundation;
**
**    This program is distributed in the hope that it will be useful,
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**    GNU General Public License for more details.
**
**    You should have received a copy of the GNU General Public License
**    along with this program; if not, write to the Free Software
**    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
*/

#include "header.h"
#include "mainwindow.h"
#include "mylistitem.h"
#include "fork.h"
#include "tabwidget.h"
#include "pageplay.h"
#include "channel.h"
#include "menubar.h"
#include "sound.h"
////slots

void
MainWindow::timerDone ()
{
   ///sound
   int value = sound->volume ();
   if (slidersound->value () != value)
      slidersound->setValue (value);
//    ///clock
//    labelclock->setText(QTime::currentTime().toString());
}


///-------------------------------------------------------
void
MainWindow::onlistitemclicked (QListViewItem *)
{
   if (tabwidget->tabToolTip (tabwidget->currentPage ()) != "play")
      return;

   static_cast < PagePlay * >(tabwidget->currentPage ())
      ->onButtonLaunchClicked ();
}

///-------------------------------------------------------



void
MainWindow::onbuttonexpandclicked ()
{
   bool flag;
   if (buttonexpand->paletteForegroundColor () == Qt::black) {
      flag = TRUE;
      buttonexpand->setPaletteForegroundColor (Qt::blue);
   }
   else {
      flag = FALSE;
      buttonexpand->setPaletteForegroundColor (Qt::black);
   }

   QListViewItem *myChild = channel->firstChild ();
   while (myChild) {
      if (flag) {
	 myChild->setOpen (TRUE);
      }
      else {
	 myChild->setOpen (FALSE);
      }

      myChild = myChild->nextSibling ();
   }
}


void
MainWindow::onbuttonsearchclicked ()
{
   if (channel->currentItem () == 0 || editsearch->text ().isEmpty ())
      return;

   bool flagfirst = TRUE;
   QListViewItem *item, *childitem, *focusitem = 0;
   ////assign initial value
   if (channel->currentItem ()->depth () == 0) {
      item = channel->currentItem ();
      childitem = item->firstChild ();
   }
   else {
      childitem = channel->currentItem ();
      item = childitem->parent ();
      childitem = childitem->nextSibling ();
   }
   /////////find focus
   while (1) {
      while (childitem) {
	 if (childitem->text (NAME_COLUMN).
	     contains (editsearch->text (), false)
	     && childitem->isEnabled ()) {
	    channel->setCurrentItem (childitem);
	    focusitem = childitem;
	    flagfirst = FALSE;
	    goto EXIT;
	 }
	 childitem = childitem->nextSibling ();
      }
      item = item->nextSibling ();
      if (!item)
	 break;
      childitem = item->firstChild ();
   }
 EXIT:;
   ////set color and find first focus if having failed finding focus so far
   item = channel->firstChild ();
   while (item) {
      bool flag = FALSE;
      childitem = item->firstChild ();
      while (childitem) {
	 if (childitem->text (NAME_COLUMN).
	     contains (editsearch->text (), false)
	     && childitem->isEnabled ()) {
	    static_cast < MyListItem * >(childitem)->bcolor = Qt::yellow;
	    childitem->repaint ();

	    flag = TRUE;
	    if (flagfirst == TRUE) {
	       channel->setCurrentItem (childitem);
	       focusitem = childitem;
	       flagfirst = FALSE;
	    }
	 }
	 else {
	    static_cast < MyListItem * >(childitem)->bcolor = Qt::white;
	    childitem->repaint ();
	 }
	 childitem = childitem->nextSibling ();
      }

      if (flag) {
	 item->setOpen (TRUE);
      }
      else {
	 item->setOpen (FALSE);
      }

      item = item->nextSibling ();
   }

//////////////////scroll to the first found item
   if (focusitem == 0)
      return;

   int value = channel->itemPos (focusitem) - channel->height () / 2;
   channel->verticalScrollBar ()->setValue (value);
}


///---------------------------------------------
void
MainWindow::updateopenstate ()
{
   QListViewItem *myChild = channel->firstChild ();
   int count = 0;
   while (myChild) {
      if (myChild->isOpen ())
	 stateopen[count++] = 1;
      else
	 stateopen[count++] = 0;

      myChild = myChild->nextSibling ();
   }
}
